/*
    弹框页面
*/
var app=require("../../../public/app");
var api=require("../../../public/thirdapi");
cc.Class({
    extends: cc.Component,

    properties: {
        itemPrefab:cc.Prefab,//获得的列表页
        content:cc.Node,
        rightbtn:cc.Node,
        leftbtn:cc.Node,
        scrollNode:cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.node.on("touchstart",function(){},this)//隐藏拼图列表右滑
        this.node.on("touchmove",function(){},this)//隐藏拼图列表右滑
        this.node.on("touchend",function(){},this)//隐藏拼图列表右滑
    },

    init (gameCtl) {
        var that=this;
        this.node.x=0;
        this.gameCtl=gameCtl;
        this.node.active=true;
        that.rightbtn.active=false;
        that.leftbtn.active=false;
        setTimeout(function(){
            that.leftbtn.active=true;
        },3000)
        this.scrollNode.on('scroll-to-bottom',this.getMinList, this);
        app.list.forEach(function(item_info,index){//弹框列表
            // console.log(index,item_info)
            that.alertNewItem(item_info,index)
        })
    },
    getMinList(){//获取拼图列表
        var that=this;
        app.getMinList({
            success(res){
                // console.log("res",res)
                if(res.data.data){
                    app.list=app.list.concat(res.data.data);
                    res.data.data.forEach((item_info,index)=>{//弹框列表
                        // console.log(index,item_info)
                        that.alertNewItem(item_info,index)
                    })
                }
                
            },
            fail(res){
                // console.log("failres",res)
            }
        })
    },
    alertNewItem(item,index){
        var that=this;
        console.log("item",item.image)
        var newItem = cc.instantiate(this.itemPrefab);
        newItem.getComponent('Item').title.string=item.title;
        newItem.getComponent('Item').info=item;


        cc.loader.load(item.image, function (err, texture) {
            // console.log("texture",texture)
            //下载头像
            newItem.getComponent('Item').avatar.getComponent(cc.Sprite).spriteFrame=new cc.SpriteFrame(texture);
            that.content.addChild(newItem);

        })

        // if(index>=this.gameCtl.list.length-1){//遍历到最后一个
        //     this.alertView.height=-poistion_y+190+20;
        //     this.alertScrollView.node.on('bounce-bottom',this.getBooks, this);
        // }
    },

    goright(){//向右滑动
        this.gameCtl.isaction=true;
        var acfinish = cc.callFunc(this.gorightFinsh, this);//动画回调
        var ac=cc.sequence(
            cc.moveTo(this.gameCtl.acspeed, 0, 0),
            acfinish
        );
        ac.easing(cc.easeIn(this.gameCtl.easspeed))
        this.node.runAction(ac);
    },
    gorightFinsh(){
        console.log("向右移动结束")
        this.gameCtl.isaction=false;
        this.rightbtn.active=false;
        this.leftbtn.active=true;
    },
    goleft(){//向右滑动
        this.gameCtl.isaction=true;
        var acfinish = cc.callFunc(this.goleftFinsh, this);//动画回调
        var ac=cc.sequence(
            cc.moveTo(this.gameCtl.acspeed, -580, 0),
            acfinish
        );
        ac.easing(cc.easeIn(this.gameCtl.easspeed))
        this.node.runAction(ac);
    },
    goleftFinsh(){
        console.log("向左移动结束")
        this.gameCtl.isaction=false;
        this.leftbtn.active=false;
        this.rightbtn.active=true;

    },

    // update (dt) {},
});
